﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZenSystemService.Api.Business;
using ZenSystemService.Api.Common.Auth;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.DTOs.Product;
using ZenSystemService.Api.DTOs.TrainingItem;
using ZenSystemService.Api.Entities;
using ZenSystemService.Api.Services;

namespace ZenSystemService.Api.Controllers
{
    /// <summary>
    ///训练项目接口
    /// </summary>
    [Route("api/products")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly ProductService _productService;

        public ProductController(ProductService productService)
        {
            _productService = productService;
        }

        /// <summary>
        /// 查询产品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ReturnObjectT<IEnumerable<ProductQueryResultDTO>>> GetProducts(ReceiveObject receiveObject)
        {
            var products = await _productService.GetProductsAsync(receiveObject);
            return products;
            // return Ok(TrainingItems);
        }

        ///// <summary>
        ///// 根据ID 获取训练项目实体信息
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpPost("GetTrainingItemById")]
        //public async Task<ReturnObjectT<InstitutionTrainingItemQueryResultDTO>> GetTrainingItemById(ReceiveObjectT<int> _receiveObjectT)
        //{
        //    ReturnObjectT<InstitutionTrainingItemQueryResultDTO> _returnObject = new ReturnObjectT<InstitutionTrainingItemQueryResultDTO>();

        //    int id = _receiveObjectT.Data;
        //    InstitutionTrainingItemQueryResultDTO trainingItem = null;// await _trainingItemService.GetTrainingItemByIdAsync(id);
        //    if (trainingItem == null)
        //    {
        //        //return NotFound();
        //        _returnObject.Code = 0;
        //        _returnObject.ErrorMsg = "获取训练项目信息失败";
        //    }
        //    else
        //    {
        //        _returnObject.Code = 1;
        //        _returnObject.Data = trainingItem;
        //    }
        //    return _returnObject;
        //    // return Ok(TrainingItem);
        //}
        ///// <summary>
        ///// 创建训练项目信息
        ///// </summary>
        ///// <param name="trainingItem"></param>
        ///// <returns></returns>
        //[HttpPost("CreateTrainingItem")]
        //public async Task<IActionResult> CreateTrainingItem(InstitutionTrainingItemCreateRequestDTO trainingItem)
        //{
        //    InstitutionTrainingItemQueryResultDTO trainingItemResult = await _trainingItemService.CreateTrainingItemAsync(trainingItem);
        //    return CreatedAtAction(nameof(GetTrainingItemById),
        //        new { id = trainingItemResult.TrainingItemId }, trainingItemResult);
        //}

        /// <summary>
        /// 根据UniqueNumber更新MAC
        /// </summary>
        /// <param name="MACs"></param>
        /// <returns></returns>
        [HttpPost("UpdateProductMAC")]
        public async Task<IActionResult> UpdateProductMAC(ProductUpdateMacRequestDTO req)
        {
            var product = await _productService.UpdateProductMacAsync(req);
            return Ok(product);
        }

        /// <summary>
        /// 根据ID更新产品信息，目前只有更新Alias的需求
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [HttpPost("UpdateProductById")]
        public async Task<IActionResult> UpdateProductById(ProductUpdateRequestDTO req)
        {
            var product = await _productService.UpdateProductByIdAsync(req);
            return Ok(product);
        }
    }
}